//
//  YMStationAnnotationIconView.swift
//  Driver
//
//  Created by lantaiyuan on 2017/11/1.
//  Copyright © 2017年 youmy. All rights reserved.
//

import UIKit
import Masonry

class YMStationAnnotationIconView: MAAnnotationView {
    
    let kWidth:CGFloat = 100.0
    let kHeight:CGFloat = 70.0
    
    var station:YMStationAnnotation = YMStationAnnotation(){
        didSet{
            let sta = station.station ?? Order_carpoolRouteStations()
            stationLb.text = sta.stationName
            peopleLb.text = "上\(sta.upNum)人,下\(sta.downNum)人"
            timeLb.text = time(intervalString: "\(sta.arriveTime)")
            calcuWH(station: sta)
        }
    }
    
    lazy var timeLb:UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 11)
        label.textColor = UIColor.init(hexString: "#666666") ?? UIColor()
        return label
    }()
    
    lazy var lineV:UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.init(hexString: "#e8e8e8") ?? UIColor()
        return view
    }()
    
    lazy var stationLb:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 11)
        label.textColor = UIColor.init(hexString: "#5298ff") ?? UIColor()
        return label
    }()
    
    lazy var peopleLb:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 11)
        label.textColor = UIColor.init(hexString: "#5298ff") ?? UIColor()
        return label
    }()
    
    override func draw(_ rect: CGRect) {
        
        let radius:CGFloat = 4
        let offset:CGFloat = 5
        let positionNum:CGFloat = rect.width / 2.0
        let changeNum = radius + offset * 2
        let w = rect.width
        let h = rect.height
        
        let context = UIGraphicsGetCurrentContext()
        
        guard let strokeColor = UIColor.init(hexString: "#e8e8e8"),
        let strokeColor2 = UIColor.init(hexString: "#5298ff")else {
            return
        }
        
        context?.setLineWidth(0.5)
        context?.setStrokeColor(strokeColor.cgColor)
        context?.setFillColor(UIColor.white.cgColor)
        
        context?.move(to: CGPoint(x: radius, y: 0))
        context?.addArc(tangent1End: CGPoint(x: w, y: 0), tangent2End: CGPoint(x: w, y: changeNum), radius: radius)
        context?.addArc(tangent1End: CGPoint.init(x: w, y: h - offset * 3), tangent2End: CGPoint.init(x: w - changeNum, y: h - offset * 3), radius: radius)
        
        context?.addLine(to: CGPoint(x: positionNum + 5, y: h - offset * 3))
        context?.addLine(to: CGPoint(x: positionNum, y: h - offset * 2))
        context?.addLine(to: CGPoint(x: positionNum - 5, y: h - offset * 3))
        
        context?.addArc(tangent1End: CGPoint.init(x: 0, y: h - offset * 3), tangent2End: CGPoint.init(x: 0, y: h - changeNum - offset), radius: radius)
        context?.addArc(tangent1End: CGPoint.init(x: 0, y: 0), tangent2End: CGPoint.init(x: radius, y: 0), radius: radius)
        context?.drawPath(using: .fillStroke)
        
        context?.setStrokeColor(strokeColor2.cgColor)
        context?.setLineWidth(1)
        context?.addArc(center: CGPoint.init(x: w/2, y: h - offset), radius: 4.0, startAngle: 0, endAngle: .pi * 2, clockwise: false)
        context?.drawPath(using: .fillStroke)
        
        super.draw(rect)
    }
    
    override init!(annotation: MAAnnotation!, reuseIdentifier: String!) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        self.centerOffset = CGPoint.init(x: 0, y: -ym_height()/2.0)
        self.bounds = CGRect(x: 0, y: 0, width: ym_width(), height: ym_height())
        
        addSubview(timeLb)
        addSubview(lineV)
        addSubview(stationLb)
        addSubview(peopleLb)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension YMStationAnnotationIconView {
    fileprivate func ym_width() -> CGFloat {
        let str = "99:99上30人,下30人" as NSString
        let width = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 11)]).width + 25
        return width
    }
    
    fileprivate func ym_height() -> CGFloat {
        let str = "99:99上30人,下30人" as NSString
        let height = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 11)]).height
        return height*2 + 7*3 + 5*3
    }
    
    fileprivate func width(_ string: String ) -> CGFloat {
        let str = string as NSString
        let w = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 11)]).width
        return w
    }
    
    fileprivate func height(_ string: String ) -> CGFloat {
        let str = string as NSString
        let h = str.size(attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 11)]).height
        return h
    }
}

extension YMStationAnnotationIconView {
    fileprivate func calcuWH(station sta:Order_carpoolRouteStations){
        let str = (timeLb.text ?? "") + (peopleLb.text ?? "")
        width = width(str) + 23
        
        let ym_timeW = width("99:99")
        timeLb.mas_makeConstraints {[weak self] (make) in
            make?.left.equalTo()(7)
            make?.centerY.mas_equalTo()(self)?.offset()(-5)
            make?.width.mas_equalTo()(ym_timeW)
        }
        
        lineV.mas_makeConstraints {[weak self] (make) in
            make?.left.equalTo()(self?.timeLb.mas_right)
            make?.top.equalTo()(5)
            make?.bottom.equalTo()(-20)
            make?.width.mas_equalTo()(0.5)
        }
        
        let ym_stationH = height("上30人,下30人")
        stationLb.mas_makeConstraints {[weak self] (make) in
            make?.left.equalTo()(self?.timeLb.mas_right)?.offset()(6)
            make?.top.equalTo()(6)
            make?.right.equalTo()(-6)
            make?.height.mas_equalTo()(ym_stationH)
        }
        
        peopleLb.mas_makeConstraints {[weak self] (make) in
            make?.left.equalTo()(self?.stationLb.mas_left)
            make?.top.equalTo()(self?.stationLb.mas_bottom)?.offset()(7)
            make?.right.equalTo()(-6)
            make?.height.mas_equalTo()(ym_stationH)
        }
    }
}

extension YMStationAnnotationIconView {
    fileprivate func time(intervalString timeString: String) -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = "HH:mm"
        
        let date = Date(timeIntervalSince1970: Double(timeString)!/1000)
        let dateString = formatter.string(from: date)
        return dateString
    }
}
